using System;
using System.Threading;
using System.Threading.Tasks;

namespace muduoDotNet.Examples
{
    class TestRunner
    {
        static void Main(string[] args)
        {
            Logger.Info("=== muduoDotNet 测试运行器 ===");

            try
            {
                TestBuffer();
                Logger.Info("✅ Buffer 测试通过");

                TestTimeStamp();
                Logger.Info("✅ TimeStamp 测试通过");

                TestLogger();
                Logger.Info("✅ Logger 测试通过");

                TestEventLoop();
                Logger.Info("✅ EventLoop 基础测试通过");

                TestTimer();
                Logger.Info("✅ Timer 测试通过");

                Logger.Info("\n🎉 所有基础测试都通过了！");;
            }
            catch (Exception ex)
            {
                Logger.Error($"❌ 测试失败: {ex.Message}");
                Logger.Error($"详细信息: {ex}");
            }

            Logger.Info("\n按任意键退出...");
            Console.ReadKey();
        }

        static void TestBuffer()
        {
            var buffer = new Buffer();
            buffer.Append("Hello, World!");
            var result = buffer.ReadAllString();
            if (result != "Hello, World!")
                throw new Exception("Buffer 测试失败");
        }

        static void TestTimeStamp()
        {
            var ts = TimeStamp.Now();
            if (!ts.IsValid)
                throw new Exception("TimeStamp 测试失败");
        }

        static void TestLogger()
        {
            Logger.Info("这是一条测试日志");
            Logger.Warning("这是一条警告日志");
            Logger.Error("这是一条错误日志");
        }

        static void TestEventLoop()
        {
            var loop = new EventLoop();
            bool taskExecuted = false;
            var taskCompletedEvent = new ManualResetEventSlim(false);

            // 在后台启动事件循环
            var loopTask = Task.Run(() => loop.Start());

            // 等待事件循环启动
            Thread.Sleep(100);

            // 添加一个任务
            loop.QueueInLoop(() =>
            {
                taskExecuted = true;
                taskCompletedEvent.Set();
            });

            // 等待任务完成
            bool completed = taskCompletedEvent.Wait(2000);
            
            // 停止事件循环
            loop.Stop();
            loopTask.Wait(1000);

            if (!completed || !taskExecuted)
                throw new Exception("EventLoop 任务执行失败");

            loop.Dispose();
        }

        static void TestTimer()
        {
            var loop = new EventLoop();
            bool timerExecuted = false;
            var timerCompletedEvent = new ManualResetEventSlim(false);

            // 在后台启动事件循环
            var loopTask = Task.Run(() => loop.Start());

            // 等待事件循环启动
            Thread.Sleep(100);

            // 添加一个短时间的定时器
            loop.AddTimer(() =>
            {
                timerExecuted = true;
                timerCompletedEvent.Set();
            }, 0.1); // 100ms

            // 等待定时器执行
            bool completed = timerCompletedEvent.Wait(1000);
            
            // 停止事件循环
            loop.Stop();
            loopTask.Wait(1000);

            if (!completed || !timerExecuted)
                throw new Exception("Timer 执行失败");

            loop.Dispose();
        }
    }
}